﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Csla;
    using System.ComponentModel;

#endregion

namespace MarketPulse.BusinessLogic.Core
{
    /// <summary>
    /// Base business class that all editable business objects i.e. those that would inherit
    /// directly from BusinessBase, should inherit from.
    /// </summary>
    /// <typeparam name="T">Must be derived from MarketPulseBase (this class)</typeparam>
    [Serializable]
    public abstract class MarketPulseBase<T> : BusinessBase<T> where T : MarketPulseBase<T>
    {
        #region Constructors

        /// <summary>
        /// Default constructor for this class
        /// </summary>
        protected MarketPulseBase()
        {
            //try
            //{
            //    this.ActionUserGUID = UserInfo.GetByUsername(Csla.ApplicationContext.User.Identity.Name).GUID;
            //}
            //catch (Exception)
            //{
                this.ActionUserGUID = new Guid("590982D5-857A-4CCB-80CC-BBC3D152EC88");
            //}

            //this.BusinessObjectGUID = new Guid("46E79E93-2C76-4D64-A313-EEA64A3469E7");
        }

        #endregion

        #region Methods

            public T Save()
            {
                //TODO Do the main object based auditing work here
                return base.Save();
            }

            #region Audit Methods

                private void AuditInsert()
                {

                }

                private void AuditUpdate()
                {

                }

                private void AuditObsolete()
                {

                }

            #endregion

            /// <summary>
            /// (Overriden) Method used to return a string representation of this BusinessObject made up of the object type and it's uniquely idetifiying Guid
            /// </summary>
            /// <returns>A string representation of the BusinessObject</returns>
            public override string ToString()
            {
                StringBuilder objectString = new StringBuilder();
                objectString.Append(typeof(T).ToString());
                objectString.Append(" GUID: [ " + GUID + " ] ");
                return objectString.ToString();
            }

            /// <summary>
            /// Method used to set the authorization rules for this business object.
            /// This is called by the Csla framework.
            /// The implementation is here in the base business class and uses the 
            /// static generic type of the derived business object to select the
            /// correct authorization information from the database.
            /// </summary>
            protected static void AddAuthorizationRules()
            {
            }

        #endregion

        #region Properties

            /// <summary>
            /// This represents the type of this BusinessObject (for auditing purposes)
            /// </summary>
            public static PropertyInfo<Nullable<Guid>> BusinessObjectGUIDProperty = RegisterProperty<Nullable<Guid>>(p => p.BusinessObjectGUID);
            /// <summary>
            /// This represents the type of this BusinessObject (for auditing purposes)
            /// </summary>
            public Nullable<Guid> BusinessObjectGUID
            {
                get { return GetProperty(BusinessObjectGUIDProperty); }
                private set { SetProperty(BusinessObjectGUIDProperty, value); }
            }

            /// <summary>
            /// This is the unique identifier used to identify all business objects within the system
            /// </summary>
            public static PropertyInfo<Nullable<Guid>> GUIDProperty = RegisterProperty<Nullable<Guid>>(p => p.GUID);
            // Marks field as true ID for an instance of this object
            /// <summary>
            /// This is the unique identifier used to identify all business objects within the system
            /// </summary>
            [DataObjectField(true, true)]
            public Nullable<Guid> GUID
            {
                get { return GetProperty(GUIDProperty); }
                private set { SetProperty(GUIDProperty, value); }
            }

            /// <summary>
            /// Boolean used to mark the object as deleted or unused (obsolete)
            /// </summary>
            public static PropertyInfo<bool> ObsoleteProperty = RegisterProperty<bool>(p => p.Obsolete);
            /// <summary>
            /// Boolean used to mark the object as deleted or unused (obsolete)
            /// </summary>
            public bool Obsolete
            {
                get { return GetProperty(ObsoleteProperty); }
                set { SetProperty(ObsoleteProperty, value); }
            }

            /// <summary>
            /// The ID of the user who initially created the object.
            /// </summary>
            public static PropertyInfo<Guid> InsertUserGUIDProperty = RegisterProperty<Guid>(p => p.InsertUserGUID);
            /// <summary>
            /// The ID of the user who initially created the object.
            /// </summary>
            public Guid InsertUserGUID
            {
                get { return GetProperty(InsertUserGUIDProperty); }
                set { SetProperty(InsertUserGUIDProperty, value); }
            }

            /// <summary>
            /// The time and date of when the object was first created
            /// </summary>
            public static PropertyInfo<SmartDate> InsertDateTimeProperty = RegisterProperty<SmartDate>(p => p.InsertDateTime);
            /// <summary>
            /// The time and date of when the object was first created
            /// </summary>
            public string InsertDateTime
            {
                get { return GetPropertyConvert<SmartDate, string>(InsertDateTimeProperty); }
                set { SetPropertyConvert<SmartDate, string>(InsertDateTimeProperty, value); }
            }

            /// <summary>
            /// The ID of the user who last caused the object to be updated.
            /// </summary>
            public static PropertyInfo<Guid> UpdateUserGUIDProperty = RegisterProperty<Guid>(p => p.UpdateUserGUID);
            /// <summary>
            /// The ID of the user who last caused the object to be updated.
            /// </summary>
            public Guid UpdateUserGUID
            {
                get { return GetProperty(UpdateUserGUIDProperty); }
                set { SetProperty(UpdateUserGUIDProperty, value); }
            }

            /// <summary>
            /// The time and date of when the object was last updated
            /// </summary>
            public static PropertyInfo<SmartDate> UpdateDateTimeProperty = RegisterProperty<SmartDate>(p => p.UpdateDateTime);
            /// <summary>
            /// The time and date of when the object was last updated
            /// </summary>
            public string UpdateDateTime
            {
                get { return GetPropertyConvert<SmartDate, string>(UpdateDateTimeProperty); }
                set { SetPropertyConvert<SmartDate, string>(UpdateDateTimeProperty, value); }
            }

            /// <summary>
            /// The ID of the user currently interacting with this BusinessObject
            /// </summary>
            public static PropertyInfo<Nullable<Guid>> ActionUserGUIDProperty = RegisterProperty<Nullable<Guid>>(p => p.ActionUserGUID);
            /// <summary>
            /// The ID of the user currently interacting with this BusinessObject
            /// </summary>
            public Nullable<Guid> ActionUserGUID
            {
                get { return GetProperty(ActionUserGUIDProperty); }
                set { SetProperty(ActionUserGUIDProperty, value); }
            }

        #endregion
    }
}
